package cn.ibizlab.odoo.core.odoo_web_tour.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iweb_tour_tour;
import cn.ibizlab.odoo.core.odoo_web_tour.domain.Web_tour_tour;
import cn.ibizlab.odoo.core.odoo_web_tour.filter.Web_tour_tourSearchContext;
import cn.ibizlab.odoo.core.odoo_web_tour.service.IWeb_tour_tourService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[向导] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Web_tour_tourServiceImpl implements IWeb_tour_tourService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iweb_tour_tourClientService web_tour_tourClientService;

    @Override
    public boolean update(Web_tour_tour web_tour_tour) {
        boolean bOk = false ;
        Iweb_tour_tour clientModel = this.convert2Model(web_tour_tour,null);
        web_tour_tourClientService.update(clientModel) ;
        this.convert2Domain(clientModel,web_tour_tour) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iweb_tour_tour clientModel = web_tour_tourClientService.createModel();
        clientModel.setId(id);
        web_tour_tourClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //web_tour_tourClientService.removeBatch(web_tour_tours) ;

    }

    @Override
    public Web_tour_tour get(Integer id) {
        Iweb_tour_tour clientModel = web_tour_tourClientService.createModel();
        clientModel.setId(id);
        web_tour_tourClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Web_tour_tour get(Web_tour_tour web_tour_tour) {
        Web_tour_tour retweb_tour_tour = this.get(web_tour_tour.getId()) ;
    	BeanCopier copier=BeanCopier.create(retweb_tour_tour.getClass(), web_tour_tour.getClass(), false);
        copier.copy(retweb_tour_tour, web_tour_tour, null);
        return web_tour_tour ;
    }

    @Override
    public void updateBatch(List<Web_tour_tour> web_tour_tours, int batchSize) {

        //web_tour_tourClientService.updateBatch(web_tour_tours,batchSize) ;

    }

    @Override
    public boolean create(Web_tour_tour web_tour_tour) {
        boolean bOk = false ;
        Iweb_tour_tour clientModel = this.convert2Model(web_tour_tour,null);
        web_tour_tourClientService.create(clientModel) ;
        this.convert2Domain(clientModel,web_tour_tour);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Web_tour_tour> web_tour_tours, int batchSize) {

                        //web_tour_tourClientService.createBatch(web_tour_tours,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Web_tour_tour> searchDefault(Web_tour_tourSearchContext context) {
        List<Web_tour_tour> list = new ArrayList<Web_tour_tour>();
        Page<cn.ibizlab.odoo.core.client.model.Iweb_tour_tour> clientModelList = web_tour_tourClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iweb_tour_tour clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Web_tour_tour>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Web_tour_tour> selectByOdoo_user(Integer id){
        Web_tour_tourSearchContext context = new Web_tour_tourSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Web_tour_tour> list = new ArrayList<Web_tour_tour>();
        Page<cn.ibizlab.odoo.core.client.model.Iweb_tour_tour> clientModelList = web_tour_tourClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iweb_tour_tour clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Web_tour_tour>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iweb_tour_tour convert2Model(Web_tour_tour domain , Iweb_tour_tour model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = web_tour_tourClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
        return model ;
    }

    public Web_tour_tour convert2Domain( Iweb_tour_tour model ,Web_tour_tour domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Web_tour_tour();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        return domain ;
    }


}
